import { Label } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { App } from '../../App';
import UtilTool from '../../tools/UtilTool';
import { loadCommonPreFab, showErrorTip, showTip, showTipBox } from '../../tools/PopuUp';
import { RoomJoinLayer } from './RoomJoinLayer';
import { Sprite } from 'cc';
import { SpriteFrame } from 'cc';
import { Button } from 'cc';
import { InputPwd } from './InputPwd';
import { MultiCommon } from './MultiCommon';
import { UIOpacity } from 'cc';
import { Animation } from 'cc';
import { returnEvent } from 'fairygui-cc/event/Event';
import { MultiMsgMgr } from './MultiMsgMgr';
const { ccclass, property } = _decorator;

enum RoomState {
    JOIN, // 可加入
    BATTLE_CAN_WATCH, // 对战中,可观战
    WAIT, // 等待中
    BATTLE_NOT_WATCH, // 对战中,不可观战
}

@ccclass('RoomItem')
export class RoomItem extends Component {

    @property(Label)
    lab_room_type : Label = null; // 游戏类型 

    @property(Label)
    lab_roomId : Label = null; // 房间号

    @property(Sprite)
    icon_lock : Sprite = null;  // 加锁图片，有密码才会加锁

    @property(Sprite)
    icon_state : Sprite = null; // 状态图片

    @property(Node)
    joinNode : Node = null; // 加入节点

    @property(Button)
    btn_watch : Button = null; // 观战按钮

    @property(Button)
    btn_wait : Button = null; // 等待按钮

    @property([Node])
    stateNodeList : Node[] = []; // 0可加入 1对战中 2等待

    data = null; 

    roomState = 0; // 状态

    roomType = 0; // 0普通场 1专业场

    start() {        
        this.setSwordAnim()
    }

    initData(data, roomType) {
        this.data = data;
        this.roomState = data.status;
        this.roomType = roomType
        
        if(data.gameType == 0) {
            this.lab_room_type.string = App.language.getLanguageLab(116)
        } else {
            this.lab_room_type.string = App.language.getLanguageLab(117)
        }

        let roomIdStr = UtilTool.getRoomNumStr(data.roomId)
        this.lab_roomId.string = UtilTool.formatStr(App.language.getLanguageLab(118), roomIdStr)

        // 锁图片
        this.icon_lock.node.active = data.havePassword

        // roomState = 0 为在等待（可加入或等待），1为游戏中
        if(this.roomState == 0) {
            // 人没满时，显示可加入
            if(this.data.currentPlayerNum < this.data.maxPlayer) {
                this.setState(RoomState.JOIN)
                
            } else {
                // 人满时，显示等待
                this.setState(RoomState.WAIT)
            }
        } else {
            if(this.data.canWatch) {
                this.setState(RoomState.BATTLE_CAN_WATCH)
            } else {
                this.setState(RoomState.BATTLE_NOT_WATCH)
            }
        }

        // 加入按钮的呼吸动画
        UtilTool.showBreatheAction(this.joinNode, true)
    }

    // 设置无数据,不可见的情况
    setHide() {
        this.node.getComponent(UIOpacity).opacity = 0
        this.joinNode.active = false
        this.btn_watch.node.active = false
        this.btn_wait.node.active = false
    }

    // 0在等待 1可观战 2可加入
    setState(state : RoomState) {

        this.joinNode.active = state == RoomState.JOIN
        this.btn_watch.node.active = state == RoomState.BATTLE_CAN_WATCH
        this.btn_wait.node.active = state == RoomState.WAIT

        this.stateNodeList[0].active = false
        this.stateNodeList[1].active = false
        this.stateNodeList[2].active = false

        switch(state) {
            case RoomState.JOIN: // 可加入
                this.stateNodeList[0].active = true
                break

            case RoomState.BATTLE_CAN_WATCH: // 对战中,可观战
                this.stateNodeList[1].active = true
                break

            case RoomState.WAIT: // 等待中
                this.stateNodeList[2].active = true
                break
        
            case RoomState.BATTLE_NOT_WATCH: // 对战中,不可观战
                this.stateNodeList[1].active = true
                break
        }

        this.setSwordAnim()
    }

    // 设置剑的动画
    setSwordAnim() {
        let swordNode : Node = this.stateNodeList[1]
        swordNode.getComponent(Animation).play("sword")
    }

    // 输入密码进入房间
    async intputPwd(cb : Function) {
        let node = await loadCommonPreFab('prefabs/hall/multiplay/InputPwd', null, false)
        let inputPwd = node.getComponent(InputPwd)
        inputPwd.show((pwd) => {
            cb && cb(pwd)
        }, this.data.roomId)
    }

    // 0可加入 1可观战 2等待
    onBtnClick(event, id) {

        UtilTool.printLog("test onBtnClick", typeof(id), id)

        id = parseInt(id)
        switch(id) {
            case 0: // 加入

                if(this.data.havePassword) {
                    let cb = (pwd)=> {
                        MultiCommon.enterRoom(this.roomType, this.data.roomId, pwd)
                    }
                    this.intputPwd(cb) // 输入密码后再进入加入房间信息界面
                } else {
                    MultiCommon.enterRoom(this.roomType, this.data.roomId, "")
                }
                break
                
            case 1: // 观战

                showTipBox({
                    content:App.language.getLanguageLab(169),
                    btnType:3,
                    func:(index)=>{
                        if(this.data.havePassword) {

                            let cb = (pwd)=> {
                                MultiMsgMgr.getInstance().sendWatchGame(this.roomType, this.data.roomId, pwd)
                                .then((data)=>{
                                    UtilTool.printLog("test sendWatchGame", data);
                                })
                            }
        
                            this.intputPwd(cb) // 输入密码后再进入观战
                            return
                        }
        
                        // 发送观战消息
                        MultiMsgMgr.getInstance().sendWatchGame(this.roomType, this.data.roomId, "")
                        .then((data)=>{
                            UtilTool.printLog("test sendWatchGame", data);
                        })
                    }
                })
                
                break

            case 2: // 等待
                break
        }
        
    }

}

